<?php
/**
 * User:ayou chow
 * Date: 2019-12-06
 * Time: 10:38
 */

namespace app\admin\controller;


use app\common\model\UserLevelRuleModel;
use app\common\model\AnchorLevelRuleModel;
use think\facade\Request;

class LevelController extends BaseController {
    public function pointlevel() {
        return $this->fetch();
    }

    public function starlevel() {
        return $this->fetch();
    }

    public function pointlevel_add() {
        return $this->fetch();
    }

    public function starlevel_edit() {
        $starlevelinfo = AnchorLevelRuleModel::get(Request::param('id'));
        $this->assign("starlevelinfo", $starlevelinfo);
        return $this->fetch();
    }

    public function getPointLevel() {
        $levels = UserLevelRuleModel::order("level", "asc")->all();
        return self::bulidSuccess($levels);
    }

    public function pointlevel_add_post() {
        $level = new UserLevelRuleModel(Request::param());
        if ($level->save()) {
            // 删除缓存
            UserLevelRuleModel::delUserLevelCache();
            //操作日志
            $log_content = "增加用户等级，ID：{$level->id}，等级：{$level->level}，经验值：{$level->point}";
            parent::addAdminLog($log_content);
            return self::bulidSuccess([]);
        }
        return self::bulidFail();
    }

    public function pointlevel_edit_post() {
        $level = UserLevelRuleModel::get(Request::param("id"));
        if (!$level) {
            return self::bulidFail("等级规则条目不存在");
        }
        $newpoint    = Request::param("point");
        $level_lower = UserLevelRuleModel::get(["level" => $level->level - 1]);
        if ($level_lower && $level_lower->point >= $newpoint) {
            return self::bulidFail("高等级所需经验值不得低于低等级");
        }
        // 记录日志
        $log_content = "修改了用户等级，ID:{$level->id}，等级：{$level->level}，经验值为：{$newpoint}，旧的经验值：{$level->point}";

        if ($level->save(["point" => $newpoint])) {
            // 删除缓存
            UserLevelRuleModel::delUserLevelCache();

            parent::addAdminLog($log_content);

            return self::bulidSuccess([]);
        }
        return self::bulidFail();
    }

    public function pointlevel_del_post() {
        $id    = Request::param('id');
        $level = UserLevelRuleModel::get($id);
        if (!$level) {
            return self::bulidFail("等级规则条目不存在");
        }

        if (UserLevelRuleModel::destroy($id)) {
            // 删除缓存
            UserLevelRuleModel::delUserLevelCache();
            // 记录日志
            $log_content = "删除用户等级，等级：{$level->level}，经验值：{$level->point}";
            parent::addAdminLog($log_content);

            return self::bulidSuccess([]);
        }
        return self::bulidFail();
    }

    public function getStarLevel() {
        $levels = AnchorLevelRuleModel::order("level", "asc")->all();
        return self::bulidSuccess($levels);
    }

    public function starlevel_edit_post() {
        $newpoint = Request::param('point');
        $level    = AnchorLevelRuleModel::get(Request::param("id"));
        if (!$level) {
            return self::bulidFail("等级规则条目不存在");
        }

        // 记录日志
        $log_content = "修改了主播星级，等级：{$level->level}，经验值为：{$newpoint}，旧的经验值：{$level->point}";

        if ($level->save(Request::param())) {
            // 删除缓存
            AnchorLevelRuleModel::delAnchorLevelCache();

            parent::addAdminLog($log_content);

            return self::bulidSuccess([]);
        }
        return self::bulidFail();
    }

    public function starlevel_del_post() {
        $id    = Request::param('id');
        $level = AnchorLevelRuleModel::get($id);
        if (!$level) {
            return self::bulidFail("等级规则条目不存在");
        }

        if (AnchorLevelRuleModel::destroy($id)) {
            // 删除缓存
            AnchorLevelRuleModel::delAnchorLevelCache();
            // 记录日志
            $log_content = "删除主播星级，等级：{$level->level}，经验值：{$level->point}";
            parent::addAdminLog($log_content);

            return self::bulidSuccess([]);
        } else {
            return self::bulidFail();
        }
    }
}